import os
from src.core.log import log
from src.core.variables.constant import CONF
from src.utils.deep import get_file_sha3_512


class Resource:
    class nginx:
        path = f"{CONF.PATH.ABS}/resource/pkg/nginx"
        # 版本
        version = "1.28.0"
        # sha3值
        sha3_512 = "7a850d70c43b351ed04b59c6d94164065d8b38802a17231c12749afbcdded23d035e004eb666be3d792cffef3d1e873e7a7892aba8a52d8d2927c0888e55d305"
        install_path = f"{CONF.PATH.ABS}/data/software/fm_nginx"
        # 默认编译参数
        default_configure_param = {
            "--prefix": install_path,
            "--build": "fm_nginx_1.28.0",
            "--with-ld-opt": "-Wl,-soname,fm_nginx_1.28.0",
            "--with-cc-opt": "-DNGX_PROCESS_NAME=fm_nginx_1.28.0",
        }

    class unzip:
        path = f"{CONF.PATH.ABS}/resource/pkg/unzip"
        # 版本
        version = "6.10b"
        # sha3值
        sha3_512 = "22f1aac1c923c5c58e7b67d8dd6deac18047ac03d9a3fbfbcb656f9671ec13103cca8cce019aa024fbacf593b292b9d66da44adcc9f3d9365b16e694663ec25a"
        install_path = f'{CONF.PATH.ABS}/data/software/fm_unzip'
        # 默认编译参数
        default_configure_param = {
            "prefix": install_path,
        }

    def __init__(self) -> None:
        list_path = f"{CONF.PATH.ABS}/resource/pkg"
        for i in os.listdir(list_path):
            if not hasattr(self, i):
                continue
            full_path = f"{list_path}/{i}"
            if get_file_sha3_512(full_path) != getattr(self, i).sha3_512:
                log.ERROR(f"缺少{i}源码包，这可能导致渔翁的部分功能异常！")
                continue

    def open(self, middleware=""):
        """打开文件"""
        match middleware:
            case "nginx":
                return open(self.nginx, "rw+")
            case _:
                return "不存在！"
